package com.example.controller;

import com.example.pojo.News;
import com.example.pojo.User;
import com.example.service.NewsService;
import com.example.service.UserService;
import com.example.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class Hello {

    @Autowired
    UserService userService;

    @Autowired
    NewsService newsService;

    @GetMapping("/hello")
    public String hello(){
        System.out.println("hello");
        return "hello world!";
    }

    @PostMapping("/hello")
    public String hello2(String name){
        return "hello1"+name;
    }

    @GetMapping("/findUser")
    public List<User> findUser(){
        return userService.findUser();
    }

    @GetMapping("/findNews")
    public Result findNews(@DefaultValue(value = "1") int currentPage,
                           @DefaultValue(value = "2")int pageSize){
        PageHelper.startPage(currentPage,pageSize);
        List<News> newsList = newsService.findNews();
        PageInfo<News> pageInfo = new PageInfo<>(newsList);

//        System.out.println(pageInfo.getTotal());
//        System.out.println(pageInfo.getPageSize());
//        System.out.println(pageInfo.getPageNum());
//        System.out.println(pageInfo .getList());

        return Result.success(pageInfo);

    }
}
